package com.yifi.system.service;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.yifi.common.DBHelper;
import com.yifi.common.SysUtil;
import com.yifi.common.base.Base1Service;
import com.yifi.common.constant.ConstantInit;
import com.yifi.common.utils.DbUtils;
import com.yifi.system.domain.MisUser;

public class MisUserService extends Base1Service {

	@Override
	public Model<?> getDao() {
		// TODO Auto-generated method stub
		return MisUser.dao;
	}

	public String getUser(String pos,String cdept)
	{
		String sql="select cname from mis_user where user_pos=? and cdept=?";
		
		return Db.queryStr(sql,pos,cdept);
	}
	public String getLeader(String cdept)
	{
		return getUser("leader",cdept);
	}
	
	public boolean addUser(String c_pro,String cdept,String user_num,String cname
			,String cphone,String cemail,String cpass)
	{
		String sql="select * from mis_user where user_num=?";
		Record record=Db.findFirst(sql,user_num);
		String ccode = "0001";// 自动编号
		boolean isedit=true;
		if(record==null)
		{
			isedit=false;
			record=new Record();
			sql = "select count(1) from mis_user where user_num='" + user_num + "'";
			
			String maxcode = DBHelper.GetString("select max(ccode) from mis_user where ccode<>'admin'");
			if (!maxcode.equals("")) {
				ccode = String.valueOf(Long.parseLong(maxcode) + 1);
				ccode = SysUtil.getFillNum(ccode, 4);
			}
			record.set("ccode", ccode);
			record.set("c_pro", c_pro);
		}
		// 区分部门与班组
		String cwork_dept = cdept;
		String cdeptgroup = DBHelper.GetString("select cgroup from mis_org where ccode='" + cdept + "'");
		if (cdeptgroup.equals("3"))// 班组部门
		{
			cwork_dept = cdept.substring(0, cdept.length() - 2);// 上级工作部门
		}
		
		record.set("cname", cname);
		record.set("user_num", user_num);
		record.set("cdept", cdept);
		record.set("cwork_dept", cwork_dept);
		record.set("cpass", cpass);
		record.set("user_pos", "");
		record.set("ctel", cphone);
		record.set("cemail", cemail);
		record.set("cphone", cphone);
		record.set("isort", "1");
		record.set("is_ld", "0");
		record.set("st_flag", "1");
		
		if(DbUtils.save("mis_user", "ccode", record, isedit))
		{
			sql="select count(1) from mis_user_post where ccode='"+ccode+"' and cpost='manager_all'";
			if(Db.queryInt(sql)<=0)
			{
				sql="insert mis_user_post (ccode,cpost,cflag,c_pro) values ('"+ccode+"','manager_all','1','"+c_pro+"')";
				Db.update(sql);
			}
			return true;
		}
		else
		{
			return false;
		}
    	
	}

	private OnLineServcie onlineServcie = new OnLineServcie();
	public void setSession(Controller controller,MisUser mUser,boolean istoken)
	{
		controller.getSession().setMaxInactiveInterval(2400*60*60);//以秒为单位		100天
		
		controller.setSessionAttr(ConstantInit.loginUser,mUser);
		/*
		String orgname=Db.queryStr("select cname from mis_org where ccode=?",mUser.getCdept());
		setSessionAttr("Gs_orgname",orgname);
		*/
		String uid=mUser.getCcode();
		
		onlineServcie.setOnLine(controller.getRequest(), uid);	//更新登录时间
		
	}

	public MisUser getMisUser(String user_num,String cpass)
	{
		String sql="select * from mis_user where user_num='"+user_num+"' and cpass='"+cpass+"'";
		return (MisUser) getDao().find(sql);
	}
}
